<?php

namespace app\modules\admin\models;

use Yii;

/**
 * This is the model class for table "access".
 *
 * @property int $id
 * @property string $title 权限名称
 * @property string $urls json 数组
 * @property int $status 状态 1：有效 0：无效
 * @property int $parent_id 父级ID
 * @property int $is_menu 是否显示为菜单
 * @property string $desc 备注
 * @property int $create_at 创建时间
 * @property int $update_at 更新时间
 */
class Access extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'access';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['parent_id', 'create_at', 'update_at'], 'integer'],
            [['title'], 'string', 'max' => 50],
            [['urls', 'desc'], 'string', 'max' => 1000],
            [['status', 'is_menu'], 'string', 'max' => 1],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => 'Title',
            'urls' => 'Urls',
            'status' => 'Status',
            'parent_id' => 'Parent ID',
            'is_menu' => 'Is Menu',
            'desc' => 'Desc',
            'create_at' => 'Create At',
            'update_at' => 'Update At',
        ];
    }

    public static function formatList(&$list)
    {
        if (!empty($list)) {
            foreach ($list as &$info) {
                $info['create_at_date'] = $info['create_at'] ? date('Y-m-d H:i:s', $info['create_at']) : '-';
                $info['update_at_date'] = $info['update_at'] ? date('Y-m-d H:i:s', $info['update_at']) : '-';
            }
        }
    }

    public static function getFirstLevelMenu()
    {
        $menu = [];
        $list = self::find()->where(['parent_id' => 0])->asArray()->all();
        if (!empty($list)) {
            $menu = array_column($list, 'title', 'id', );
        }
        return $menu;
    }

}
